package com.lau.common.exception;

import com.lau.common.constant.ConstEnum;
import com.lau.common.vo.ResponseVo;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author mohan lau
 * @version 1.0
 * @description: TODO
 * @classname 异常处理
 * @date 2021/10/30 21:14
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseVo ExceptionHandler(Exception e){
        if (e instanceof GlobalException){
            GlobalException e1 = (GlobalException) e;
            return ResponseVo.error(ConstEnum.ERROR,e1.getMsg());
        }else if (e instanceof BindException){
            BindException e1 = (BindException) e;
            return ResponseVo.error(ConstEnum.BIND_EXCEPTION,e1.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        }
        return ResponseVo.error(ConstEnum.ERROR);
    }

//    @ResponseStatus(HttpStatus.OK)
//    public CallResultMsg sendSuccessResponse(){
//        return new CallResultMsg(true, CodeAndMsg.SUCCESS, null);
//    }
//
//    @ResponseStatus(HttpStatus.OK)
//    public CallResultMsg sendSuccessResponse(T data) {
//        return new CallResultMsg(true, CodeAndMsg.SUCCESS, data);
//    }
//
//    @ExceptionHandler(UserDefinedException.class)
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    public CallResultMsg sendErrorResponse_UserDefined(Exception exception){
//        return new CallResultMsg(false, ((UserDefinedException)exception).getException(), null);
//    }
//
//    @ExceptionHandler(Exception.class)
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    public CallResultMsg sendErrorResponse_System(Exception exception){
//        if (exception instanceof UserDefinedException) {
//            return this.sendErrorResponse_UserDefined(exception);
//        }
//
//        return new CallResultMsg(false, CodeAndMsg.UNKNOWEXCEPTION, null);
//    }
}
